package com.yupi.springbootinit.utils;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.yupi.springbootinit.bizmq.BiMqConstant;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class mqAopnoAspect {

    @Before("execution(* com.yupi.springbootinit.controller..*(..))")
    public void Before() {
        try{
            ConnectionFactory factory=new ConnectionFactory();
            factory.setHost("localhost");
            factory.setPort(5672);
            //设置账号信息，用户名，密码，vhost
            factory.setVirtualHost("testhost");
            factory.setUsername("admin");
            factory.setPassword("123456");
            Connection connection=factory.newConnection();
            Channel channel= connection.createChannel();

            String EXCHANGE_NAME= BiMqConstant.BI_EXCHANGE_NAME;
            channel.exchangeDeclare(EXCHANGE_NAME,"direct");

            String queueName=BiMqConstant.BI_QUEUE_NAME;
            channel.queueDeclare(queueName,true,false,false,null);
            channel.queueBind(queueName,EXCHANGE_NAME,BiMqConstant.BI_ROUTING_KEY);

        }catch (Exception e){

        }
    }
}
